package edu.gdkm.ssm.dao;

import edu.gdkm.ssm.domain.Permission;
import edu.gdkm.ssm.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 角色查询仓储
 */
public interface IRoleDao {

    /**
     * 根据用户id查询出所有对应的角色
     */
    @Select("select * from role where id in (select roleId from users_role where userId=#{userId})")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions", column = "id", javaType = java.util.List.class, many = @Many(select = "edu.gdkm.ssm.dao.IPermissionDao.findPermissionByRoleId"))
    })
    List<Role> findRoleByUserId(String userId) throws Exception;

    /**
     * 查找所有的角色信息
     * @return  角色信息
     */
    @Select("select * from role")
    List<Role> findAll();

    /**
     * 插入新的角色信息
     * @param role  角色信息
     */
    @Insert("insert into role(id, roleName, roleDesc) value (replace(uuid(), '-', ''), #{roleName}, #{roleDesc})")
    void save(Role role);

    /**
     * 查找当前用户可以添加其他角色的信息
     * @param id 当前用户id
     * @return
     */
    @Select("select * from role where id not in (select roleId from users_role where userId=#{userId})")
    List<Role> findOtherRoles(String id);

    /**
     * 根据角色id查找角色信息
     * @param roleId 角色id
     * @return 角色信息
     */
    @Select("select * from role where id=#{roleId}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions", column = "id", javaType = java.util.List.class, many = @Many(select = "edu.gdkm.ssm.dao.IPermissionDao.findPermissionByRoleId"))
    })
    Role findById(String roleId);

    /**
     * 给当前角色查找其他可添加的权限资源
     * @param roleId 当前角色id
     * @return 其他可添加的权限资源
     */
    @Select("select * from permission where id not in (select permissionId from role_permission where roleId=#{roleId})")
    List<Permission> findOtherPermissions(String roleId);

    /**
     * 给当前角色添加多个权限资源
     * @param roleId 角色id
     * @param permissionIds 权限资源id
     */
    @Insert("insert into role_permission(roleId,permissionId) values(#{roleId}, #{permissionId})")
    void addPermissionToRole(@Param("roleId") String roleId, @Param("permissionId") String permissionId);

    /**
     * 根据角色id删除当前角色信息
     * @param roleId 角色id
     */
    @Delete("delete from role where id=#{roleId}")
    void deleteRoleById(String roleId);

    /**
     * 根据角色id删除用户与角色表关联表信息
     * @param roleId 角色id
     */
    @Delete("delete from users_role where roleId=#{roleId}")
    void deleteFromUser_RoleByRoleId(String roleId);

    /**
     * 根据角色id删除角色与权限关联表信息
     * @param roleId 角色id
     */
    @Delete("delete from role_permission where roleId=#{roleId}")
    void deleteFromRole_PermissionByRoleId(String roleId);
}
